<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #container{
            margin: 0 auto;
            width:80%;
            margin-top: 10px;
        }
        .boxOne{
            border: 1px solid #000;
            height: 40px;
            box-sizing: border-box;
            position:relative;
        }
        .boxOne span{
            top: 20%;
            position: absolute;
            left: 0;
        }
        #goods{
            border:1px solid salmon;
            text-align: center;
            width: 100%;
            margin-top: 10px;
        }
        #goods tr{
            height: 35px;
        }
        #goods tr>th{
            background-color: aqua;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <div id="app">
        <div id="container">
            <div id="addTag"  class="boxOne">
                <span>
                    <label>品牌名称</label>
                    <input type="text" v-model="goodName">
                    <button @click="addGoods">添加</button>
                </span>
            </div>
            <div id="searchTag" class="boxOne">
                <span>
                    <label>品牌名称</label>
                    <input type="text" v-model="findGoodName">
                    <button @click="getGoodList">搜索</button>
                </span>
            </div>

            <table id="goods">
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>商品名称</th>
                        <th>价格</th>
                        <th>数量</th>
                        <th>添加时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(value,index) in goodList" :key="index">
                        <td>{{index+1}}</td>
                        <td>{{value.name}}</td>
                        <td>{{value.yuan}}</td>
                        <td>
                            <button @click="handleReduce(index)">-</button>
                            {{value.num}}
                        <button @click="handleAdd(index)">+</button>
                        </td>
                        <td>{{value.time}}</td>
                        <td><a href="#" @click="delete1($event)">删除</a></td>
                    </tr>
                </tbody>
            </table>
            <div>价格总计是：{{totalPrice}}</div>
        </div>
    </div>
    <script>
        var vm= new Vue({
            el:'#app',
            data:{
                goodList:[
                    {name:"水果",yuan:50,num:10,time:"2022/12/9"},
                    {name:"蔬菜",yuan:50,num:10,time:"2022/12/9"},
                    {name:"肉类",yuan:50,num:10,time:"2022/12/9"}
                ],
                goodName:'',
                findGoodName:'',

            },
            methods:{
                addGoods(){
                    var goodObj=new Object();
                    goodObj.name=this.goodName;
                    var current_time=new Date().toLocaleString();
                    goodObj.time = current_time;
                    goodObj.yuan=20;
                    goodObj.num=1;
                    this.goodList.push(goodObj);

                },
                getGoodList(){
                    var tarGoodName=this.findGoodName;
                    var tempGoodList=new Array();
                    for(good of this.goodList){
                        if(good.name==tarGoodName){
                            tempGoodList.push(good)
                        }
                    }
                    this.goodList=tempGoodList;
                },
                handleReduce(index){
                    if(this.getGoodList[index].num==1) return;
                    this.goodList[index].num--;

                },
                handleAdd(index){
                    this.goodList[index].num++;

                },
                delete1(event){
                    if (confirm('是否删除？')) {
                        this.goodList.splice(event.data,1)
                    }
                    
                    //splice(2)
                    //0,1,2,3,4,5 ----0,1
                     //splice(-2)
                    //0,1,2,3,4,5 ----4,5
                    //splice(index,howmany)
                     //splice(2,1)
                     //0,1,2,3,4,5  --2

                }


            },
            computed:{
                totalPrice(){
                    var total=0;
                    for(var i=0;i<this.goodList.length;i++){
                        var item=this.goodList[i];
                        total +=item.yuan*item.num;
                    }
                    return total;
                }
            }
       
        })
    </script>
</body>
</html>